﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;

namespace Vehicle.Web.Pages.Post
{
    /// <summary>
    /// Copyright (C) M-Tear团队
    /// 文 件 名：Main.aspx.cs
    /// 版    本：v1.00.0000
    /// 创建标识：2009-09-03   Created by 孙成杰 V1.00.0000
    /// 功能说明：公告管理页面类
    /// 注意事项：无
    /// 
    /// 更新记录：
    /// 
    /// </summary>
    public partial class Main : Vehicle.Web.Component.PageBase
    {
        #region ---基本变量实例化---
        BLL.Post b_Post = new Vehicle.BLL.Post();//实例化Post逻辑层
        BLL.Log b_Log = new Vehicle.BLL.Log();
        Model.ComponentsInfo m_ComponentsInfo = new Vehicle.Model.ComponentsInfo();
        public string onshow;//定义没有数据时页面的显示内容
        #endregion

        #region ---基本操作---
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                lbtn_AllDelete.Attributes.Add("onClick", "return   confirm('是否要删除该记录？');");
                RepeaterBind();//绑定数据
               
            }
        }
        /// <summary>
        /// 全部删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtn_AllDelete_Click(object sender, EventArgs e)
        {
            int Count = 1;
            foreach (RepeaterItem item in Rept.Items)
            {
                CheckBox chk = (CheckBox)item.FindControl("Chk_Num");
                if (chk.Checked)
                {
                    Count++; 
                    b_Log.Add(new Vehicle.Model.LogInfo(session_ID, DateTime.Now, "成功删除编号为" +chk.ToolTip + "的公告信息"));//添加日志
                    b_Post.Delete(new Model.ComponentsInfo("Post", "P_ID", chk.ToolTip));//根据ID删除公告
                   
                }

            }
            if (Count > 1)
            {
                ScriptManager.RegisterClientScriptBlock(UPanel, this.GetType(), "click", "alert('删除成功！')", true);
                RepeaterBind();
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(UPanel, this.GetType(), "click", "alert('您还没有进行选择！')", true);
            }
        }


        /// <summary>
        /// 分页控件分页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            RepeaterBind();
        }

        /// <summary>
        /// 通过时间和标题查询分页控件和Datalist数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Select_P_Time_Click(object sender, EventArgs e)
        {
            RepeaterBind();
        }
        /// <summary>
        /// 分页控件和Repeater数据绑定
        /// </summary>
        public void RepeaterBind()
        {
            this.Anp_Page.RecordCount = b_Post.GetAllSearchCount(new Model.ComponentsInfo("Post", "1", "1", "P_Time", "P_Title", this.txt_P_Title.Text.Trim(), Common.TextHelper.StringToDateTime((this.Gmdp_Start.DateString)), Common.TextHelper.StringToDateTime((this.Gmdp_End.DateString)), "P_ID"));//根据公告标题进行绑定获得总数
            m_ComponentsInfo.startIndex = Anp_Page.StartRecordIndex;//当前页的起始索引
            m_ComponentsInfo.endIndex = Anp_Page.EndRecordIndex;//当前页的结束索引
            m_ComponentsInfo.TCount = this.txt_P_Title.Text.Trim();//时间
            m_ComponentsInfo.StartTime = Common.TextHelper.StringToDateTime(this.Gmdp_Start.DateString);//开始时间
            m_ComponentsInfo.EndTime = Common.TextHelper.StringToDateTime(this.Gmdp_Start.DateString);//结束时间
            b_Post.RepeaterBindSearch(Rept, m_ComponentsInfo);//绑定Repeater控件
            if (Rept.Items.Count == 0)//判断没有数据时页面的显示内容
            {
                onshow = "当前页面无数据";
            }
            else
            {
                onshow = "";
            }
        }
        /// <summary>
        /// ReptComm激发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        protected void Rept_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {

                b_Post.Delete(new Model.ComponentsInfo("Post", "P_ID", e.CommandArgument.ToString()));//删除公告
                b_Log.Add(new Vehicle.Model.LogInfo(session_ID, DateTime.Now, "成功删除编号为" + e.CommandArgument.ToString() + "的公告信息"));//添加日志
                ScriptManager.RegisterClientScriptBlock(UPanel, this.GetType(), "click", "alert('删除成功！')", true);
                RepeaterBind();
            }
        }
        /// <summary>
        /// Rept绑定数据事件
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void Rept_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                ((LinkButton)(e.Item.FindControl("Lbtn_Delete"))).Attributes.Add("onClick", "return   confirm('是否要删除该记录？');");
            }
        }

        #endregion

    }
}
